package com.atguigu.campusintelligentassistantsystem.service;

import com.atguigu.campusintelligentassistantsystem.entity.ActivityRegistration;
import com.atguigu.campusintelligentassistantsystem.entity.CampusActivity;
import com.atguigu.campusintelligentassistantsystem.repository.ActivityRegistrationRepository;
import com.atguigu.campusintelligentassistantsystem.repository.CampusActivityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class ActivityService {

    @Autowired
    private CampusActivityRepository campusActivityRepository;

    @Autowired
    private ActivityRegistrationRepository activityRegistrationRepository;

    /**
     * 发布校园活动
     * @param activity 活动信息
     * @return 保存后的活动
     */
    public CampusActivity publishActivity(CampusActivity activity) {
        activity.setPublishTime(LocalDateTime.now());
        activity.setCurrentParticipants(0); // 初始参与人数为0
        return campusActivityRepository.save(activity);
    }

    /**
     * 获取所有活动
     * @return 活动列表
     */
    public List<CampusActivity> getAllActivities() {
        return campusActivityRepository.findAll();
    }

    /**
     * 根据状态获取活动
     * @param status 状态
     * @return 活动列表
     */
    public List<CampusActivity> getActivitiesByStatus(String status) {
        return campusActivityRepository.findByStatusOrderByActivityTime(status);
    }

    /**
     * 根据ID获取活动
     * @param id 活动ID
     * @return 活动信息
     */
    public CampusActivity getActivityById(Long id) {
        Optional<CampusActivity> activity = campusActivityRepository.findById(id);
        return activity.orElse(null);
    }

    /**
     * 更新活动信息
     * @param activity 活动信息
     * @return 更新后的活动
     */
    public CampusActivity updateActivity(CampusActivity activity) {
        return campusActivityRepository.save(activity);
    }

    /**
     * 删除活动
     * @param id 活动ID
     */
    public void deleteActivity(Long id) {
        campusActivityRepository.deleteById(id);
    }

    /**
     * 学生报名活动
     * @param activityId 活动ID
     * @param studentId 学生ID
     * @return 报名记录
     */
    public ActivityRegistration registerForActivity(Long activityId, Long studentId) {
        // 检查是否已经报名
        ActivityRegistration existingRegistration = activityRegistrationRepository.findByActivityIdAndStudentId(activityId, studentId);
        if (existingRegistration != null) {
            return existingRegistration; // 已经报名
        }

        Optional<CampusActivity> activityOpt = campusActivityRepository.findById(activityId);
        if (activityOpt.isPresent()) {
            CampusActivity activity = activityOpt.get();
            // 检查是否还有名额
            if (activity.getMaxParticipants() == null || activity.getCurrentParticipants() < activity.getMaxParticipants()) {
                // 更新活动参与人数
                activity.setCurrentParticipants(activity.getCurrentParticipants() + 1);
                campusActivityRepository.save(activity);

                // 创建报名记录
                ActivityRegistration registration = new ActivityRegistration();
                registration.setActivityId(activityId);
                registration.setStudentId(studentId);
                registration.setRegistrationTime(LocalDateTime.now());
                registration.setStatus("已报名");
                return activityRegistrationRepository.save(registration);
            }
        }
        return null; // 报名失败
    }

    /**
     * 取消活动报名
     * @param activityId 活动ID
     * @param studentId 学生ID
     * @return 操作结果
     */
    public boolean cancelRegistration(Long activityId, Long studentId) {
        ActivityRegistration registration = activityRegistrationRepository.findByActivityIdAndStudentId(activityId, studentId);
        if (registration != null) {
            // 更新报名状态
            registration.setStatus("已取消");
            activityRegistrationRepository.save(registration);

            // 更新活动参与人数
            Optional<CampusActivity> activityOpt = campusActivityRepository.findById(activityId);
            if (activityOpt.isPresent()) {
                CampusActivity activity = activityOpt.get();
                if (activity.getCurrentParticipants() > 0) {
                    activity.setCurrentParticipants(activity.getCurrentParticipants() - 1);
                    campusActivityRepository.save(activity);
                }
            }
            return true;
        }
        return false;
    }

    /**
     * 根据活动ID获取报名记录
     * @param activityId 活动ID
     * @return 报名记录列表
     */
    public List<ActivityRegistration> getRegistrationsByActivityId(Long activityId) {
        return activityRegistrationRepository.findByActivityId(activityId);
    }

    /**
     * 根据学生ID获取报名记录
     * @param studentId 学生ID
     * @return 报名记录列表
     */
    public List<ActivityRegistration> getRegistrationsByStudentId(Long studentId) {
        return activityRegistrationRepository.findByStudentId(studentId);
    }
}